package cn._51doit.day06;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.TimeCharacteristic;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.timestamps.BoundedOutOfOrdernessTimestampExtractor;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;

/**
 * 先keyBy，再按照EventTime划分滚动窗口
 * 设置延迟时间大于0
 *
 * WaterMark = 产生WaterMark对应的DataStream每个分区最大的EventTime - 延迟时间
 *
 * 我的小总结: 并行度为4 滚动窗口长度10秒,延迟时间2秒
 *  老的API触发时机是[0,9999] 或者[0,10000)
 *  下面设置的延迟时间是2秒,所以触发时机是 [0,11999] 或者[0,12000)
 *  但是因为是4并行度,而且数据是轮询的方法从上游发送给下游
 *  数据轮询的发送到四个WarterMark产生区,然后这WarterMark广播加轮询的方式发送到下游分区
 *  每个下游分区会记录最小的WarterMark,当四个WarterMark都到达触发线后,才会触发窗口
 *
 *  就比如下面的数据
 *  5000,a,1
 * 11999,a,2
 * 11999,a,2
 * 11999,a,2
 * 11999,a,2
 *  第一个时间戳是5000,获得触发条件[0,11999] 或者[0,12000) ,因为有延迟时间
 *  然后11999的数据由第二个warterMark获取时间戳信息,广播发送到下游,然后下游所有分区记录下来,第二个分区触发了
 *  然后11999的数据由第三个warterMark获取时间戳信息,广播发送到下游,然后下游所有分区记录下来,第三个分区触发了
 *  然后11999的数据由第四个warterMark获取时间戳信息,广播发送到下游,然后下游所有分区记录下来,第四个分区触发了
 *  然后11999的数据由第一个warterMark获取时间戳信息,广播发送到下游,然后下游所有分区记录下来,第一个分区触发了
 * ok,下游触发窗口,进行数据的计算  获得数据5000,a,1
 * 然后
 * 21999,a,3
 * 21999,a,3
 * 21999,a,3
 * 21999,a,3
 * 也是一样,下游触发窗口,进行数据的计算  获得数据11999,a,8
 */
public class EventTimeTumblingWindowLateTriggerDemo {

    public static void main(String[] args) throws Exception {

        Configuration configuration = new Configuration();
        configuration.setInteger("rest.port", 8081);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(configuration);

        //使用老的API
        env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);
        //5000,spark,3
        //7000,spark,3
        DataStreamSource<String> lines = env.socketTextStream("doit01", 8888);

        SingleOutputStreamOperator<Tuple3<Long, String, Integer>> tpStream = lines.map(new MapFunction<String, Tuple3<Long, String, Integer>>() {
            @Override
            public Tuple3<Long, String, Integer> map(String line) throws Exception {
                String[] fields = line.split(",");
                long ts = Long.parseLong(fields[0]);
                String word = fields[1];
                int count = Integer.parseInt(fields[2]);
                return Tuple3.of(ts, word, count);
            }
        });

        //提取数据中的时间
        //（触发EventTime类型窗口执行的信号机制）
        //设置窗口延迟触发的时间为2秒
        SingleOutputStreamOperator<Tuple3<Long, String, Integer>> wordAndCountWithMaterMark = tpStream.assignTimestampsAndWatermarks(new BoundedOutOfOrdernessTimestampExtractor<Tuple3<Long, String, Integer>>(Time.seconds(2)) {
            @Override
            public long extractTimestamp(Tuple3<Long, String, Integer> tp) {
                return tp.f0;
            }
        });

        //先调用keyBy
        KeyedStream<Tuple3<Long, String, Integer>, String> keyedStream = wordAndCountWithMaterMark.keyBy(t -> t.f1);

        //划分EventTime类型的窗口
        WindowedStream<Tuple3<Long, String, Integer>, String, TimeWindow> windowedStream = keyedStream.window(TumblingEventTimeWindows.of(Time.seconds(10)));

        //调用window function
        SingleOutputStreamOperator<Tuple3<Long, String, Integer>> res = windowedStream.sum(2);

        res.print();
        env.execute();

    }
}
